﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace NNDC
{
    public partial class Users : Form
    {
        public Users()
        {
            InitializeComponent();
        }
        public void BindData()
        {
            SqlConnection conn = data.DBConn.Cycon();
            SqlDataAdapter sda = new SqlDataAdapter("select WaiterName,CardNum,WaiterNum,Sex,Age,tel,ID from tb_Waiter order by ID desc",conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }
        private void Users_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            BindData();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtName.Text = dataGridView1.SelectedCells[0].Value.ToString();
            txtsfz.Text = dataGridView1.SelectedCells[1].Value.ToString();
            txtNum.Text = dataGridView1.SelectedCells[2].Value.ToString();
            comboBox1.SelectedItem = dataGridView1.SelectedCells[3].Value.ToString().Trim();
            txtRox.Text = dataGridView1.SelectedCells[4].Value.ToString();
            txtPhone.Text = dataGridView1.SelectedCells[5].Value.ToString();
            button2.Enabled = true;
            button6.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtsfz.Text = "";
            txtNum.Text = "";
            txtRox.Text = "";
            txtPhone.Text = "";
            txtName.Enabled = true;
            txtNum.Enabled = true;
            txtPhone.Enabled = true;
            txtRox.Enabled = true;
            txtsfz.Enabled = true;
            comboBox1.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = true;
            button2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button3.Enabled = true;
            button4.Enabled = true;
            txtName.Enabled = false;
            txtNum.Enabled = true;
            txtsfz.Enabled = true;
            this.comboBox1.Enabled = true;
            txtRox.Enabled = true;
            txtPhone.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection conn = data.DBConn.Cycon();
            conn.Open();
            SqlCommand cmd = new SqlCommand("select count(*) from tb_Waiter where WaiterName='"+txtName.Text+"'",conn);
            int i = Convert.ToInt32(cmd.ExecuteScalar());
            if (i > 0)
            {
                cmd = new SqlCommand("update tb_Waiter set WaiterName='"
                    + txtName.Text + "',CardNum='" + txtsfz.Text + "',WaiterNum='"
                    + txtNum.Text + "',Sex='" + comboBox1.SelectedItem.ToString()
                    + "',Age='" + txtRox.Text + "',tel='" + txtPhone.Text + "'where ID='"
                    + dataGridView1.SelectedCells[6].Value.ToString() + "'", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                BindData();
                button1.Enabled = true;
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = true;
                button6.Enabled = false;
                button7.Enabled = true;
                txtName.Enabled = false;
            }
            else
            {
                cmd = new SqlCommand("insert into tb_Waiter(WaiterName,CardNum,WaiterNum,Sex,Age,Tel) values('"
                    + txtName.Text + "','" + txtsfz.Text + "','" + txtNum.Text + "','"
                    + comboBox1.SelectedItem.ToString() + "','" + txtRox.Text + "','"
                    + txtPhone.Text+ "')", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                BindData();
                button1.Enabled = true;
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = true;
                button6.Enabled = false;
                button7.Enabled = true;
                txtName.Enabled = false;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button1.Enabled = true;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button6.Enabled = false;
            txtName.Enabled = false;
            txtNum.Enabled = false;
            txtPhone.Enabled = false;
            txtRox.Enabled = false;
            txtsfz.Enabled = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            SqlConnection conn = data.DBConn.Cycon();
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete from tb_Waiter where ID='" + dataGridView1.SelectedCells[6].Value.ToString() + "'", conn);
            cmd.ExecuteNonQuery();
            BindData();
        }
    }
}
